package cn.tedu.ivos.geofence.service.impl;

import cn.tedu.ivos.base.exception.ServiceException;
import cn.tedu.ivos.base.response.StatusCode;
import cn.tedu.ivos.geofence.mapper.GeofenceMapper;
import cn.tedu.ivos.geofence.pojo.dto.GeofenceParam;
import cn.tedu.ivos.geofence.pojo.dto.GeofenceQuery;
import cn.tedu.ivos.geofence.pojo.entity.Geofence;
import cn.tedu.ivos.geofence.pojo.vo.GeofenceVO;
import cn.tedu.ivos.geofence.service.GeofenceService;
import cn.tedu.ivos.vehicle.mapper.VehicleMapper;
import cn.tedu.ivos.vehicle.pojo.dto.VehicleQuery;
import cn.tedu.ivos.vehicle.pojo.vo.VehicleVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class GeofenceServiceImpl implements GeofenceService {
    @Autowired
    private GeofenceMapper geofenceMapper;
    @Autowired
    private VehicleMapper vehicleMapper;

    /**
     * 查询电子围栏
     *
     * @param geofenceQuery
     * @return
     */
    @Override
    public List<GeofenceVO> selectGeofence(GeofenceQuery geofenceQuery) {
        log.debug("查询电子围栏业务geofenceQuery={}", geofenceQuery);
        List<GeofenceVO> list = geofenceMapper.selectGeofence(geofenceQuery);
        for (int i = 0; i < list.size(); i++) {
            GeofenceVO geofenceVO = list.get(i);
            VehicleQuery vehicleQuery = new VehicleQuery();
            vehicleQuery.setGeofenceId(geofenceVO.getId());
            List<VehicleVO> vehicleVOList = vehicleMapper.selectVehicle(vehicleQuery);
            // 获取当前围栏绑定车辆数量
            int totalNum = vehicleVOList.size();
            // 可用车辆数
            int availableNum = 0;
            for (VehicleVO vehicleVO : vehicleVOList) {
                if (vehicleVO.getStatus().equals("1")) {
                    availableNum++;
                }
            }
            if (availableNum>0){
                geofenceVO.setAvailableNum(availableNum);
            }else{
                geofenceVO.setAvailableNum(0);
            }
            geofenceVO.setTotalNum(totalNum);
            geofenceVO.setVehicleList(vehicleVOList);
        }
        return list;
    }

    /**
     * 新建电子围栏信息和修改围栏状态
     *
     * @param geofenceParam
     */
    @Override
    public void saveGeofence(GeofenceParam geofenceParam) {
        log.debug("保存围栏信息业务:geofenceParam={}", geofenceParam);
        Geofence geofence = new Geofence();
        BeanUtils.copyProperties(geofenceParam, geofence);
        if (geofence.getId() != null) {
            geofence.setUpdateTime(new Date());
            geofenceMapper.update(geofence);
        } else {
            geofence.setStatus("1");//启用状态
            geofence.setCreateTime(new Date());
            geofenceMapper.insert(geofence);
        }
    }


    /**
     * 删除围栏信息按钮
     *
     * @param geofenceId
     */
    @Override
    public void deleteGeofence(Long geofenceId) {
        log.debug("删除围栏业务geofenceId={}", geofenceId);
        // 需要判断围栏该围栏是否有绑定的车辆，如果有绑定的车辆不能删除
        VehicleQuery vehicleQuery = new VehicleQuery();
        vehicleQuery.setGeofenceId(geofenceId);
        List<VehicleVO> vehicleList = vehicleMapper.selectVehicle(vehicleQuery);
        // 判断是否有车辆
        if (vehicleList.size() > 0 && vehicleList != null) {
            throw new ServiceException(StatusCode.OPERATION_FAILED);
        } else {
            // 删除车辆
            geofenceMapper.deleteById(geofenceId);
        }
    }

}
